package com.carolouvindam;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.carolouvindam.Model.Order;
import com.carolouvindam.View.PriorityButton;

import java.util.List;

public class ListViewOrderAdapter extends ArrayAdapter<Order> {

    int resource;

    public ListViewOrderAdapter(Context context, int textViewResourceId, List<Order> objects) {
        super(context, textViewResourceId, objects);
        this.resource = textViewResourceId;
    }

    // On override ArrayAdapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout orderView;
        // Récuperer l'order courant
        Order order = getItem(position);

        // Inflate the view
        if (convertView == null) {
            orderView = new LinearLayout(getContext());
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(inflater);
            vi.inflate(resource, orderView, true);
        } else {
            orderView = (LinearLayout) convertView;
        }

        // Récupérer les View des orderView
        TextView clientName = (TextView) orderView.findViewById(R.id.textViewClientName);
        TextView numberOfProducts = (TextView) orderView.findViewById(R.id.textViewNumberOfProducts);
        TextView clientAdress = (TextView) orderView.findViewById(R.id.textViewClientAdress);
        TextView phoneNumber = (TextView) orderView.findViewById(R.id.textViewPhoneNumber);
        PriorityButton priority = (PriorityButton) orderView.findViewById(R.id.button);

        // Remplir les taskView
        clientName.setText(order.getClient().getFirstName() + " " + order.getClient().getName());
    //    numberOfProducts.setText(order.getProduits().size());
        clientAdress.setText(order.getAddress().toString());
        phoneNumber.setText(order.getClient().getPhoneNumber());
        priority.setValue(order.getPriority());


        return (orderView);


    }


}
